package com.kczyjg.hybird;

/**
 * 坐标系转换
 *
 * @author zuozhijie
 * @date 2020/6/15 17:02
 */
public class TransformUtil {
    private static double A = 6378245.0d;
    private static double Ee = 0.006693421622965943d;
    private static double Pi = 3.141592653589793d;
    public static double XPi = 52.35987755982988d;

    public static double[] Gcj2Bd(double d, double d2) {
        double sqrt = Math.sqrt((d2 * d2) + (d * d)) + (2.0E-5d * Math.sin(XPi * d));
        double atan2 = Math.atan2(d, d2) + (3.0E-6d * Math.cos(XPi * d2));
        return new double[]{(sqrt * Math.sin(atan2)) + 0.006d, (Math.cos(atan2) * sqrt) + 0.0065d};
    }

    public static double[] Wgs2Gcj(double d, double d2) {
        double transformLat = transformLat(d2 - 105.0d, d - 35.0d);
        double transformLon = transformLon(d2 - 105.0d, d - 35.0d);
        double d3 = (d / 180.0d) * Pi;
        double sin = Math.sin(d3);
        double d4 = 1.0d - (sin * (Ee * sin));
        double sqrt = Math.sqrt(d4);
        double d5 = (transformLat * 180.0d) / (((A * (1.0d - Ee)) / (d4 * sqrt)) * Pi);
        return new double[]{d5 + d, ((transformLon * 180.0d) / ((Math.cos(d3) * (A / sqrt)) * Pi)) + d2};
    }

    public static double[] bd2Gcj(double d, double d2) {
        double d3 = d2 - 0.0065d;
        double d4 = d - 0.006d;
        double sqrt = Math.sqrt((d3 * d3) + (d4 * d4)) - (2.0E-5d * Math.sin(XPi * d4));
        double atan2 = Math.atan2(d4, d3) - (Math.cos(d3 * XPi) * 3.0E-6d);
        double cos = Math.cos(atan2) * sqrt;
        return new double[]{Math.sin(atan2) * sqrt, cos};
    }

    public static double[] delta(double d, double d2) {
        if (outOfChina(d, d2)) {
            return new double[]{d, d2};
        }
        double transformLat = transformLat(d2 - 105.0d, d - 35.0d);
        double transformLon = transformLon(d2 - 105.0d, d - 35.0d);
        double d3 = (d / 180.0d) * Pi;
        double sin = Math.sin(d3);
        double d4 = 1.0d - (sin * (Ee * sin));
        double sqrt = Math.sqrt(d4);
        return new double[]{(transformLat * 180.0d) / (((A * (1.0d - Ee)) / (d4 * sqrt)) * Pi), (180.0d * transformLon) / (((A / sqrt) * Math.cos(d3)) * Pi)};
    }

    public static double[] gcj02ToGps84(double d, double d2) {
        double[] delta = delta(d, d2);
        return new double[]{d - delta[0], d2 - delta[1]};
    }

    public static double[] gps84ToGcj02(double d, double d2) {
        double[] delta = delta(d, d2);
        return new double[]{delta[0] + d, delta[1] + d2};
    }

    public static boolean outOfChina(double d, double d2) {
        return d2 < 72.004d || d2 > 137.8347d || d < 0.8293d || d > 55.8271d;
    }

    private static double transformLat(double d, double d2) {
        return -100.0d + (2.0d * d) + (3.0d * d2) + (0.2d * d2 * d2) + (0.1d * d * d2) + (0.2d * Math.sqrt(Math.abs(d))) + ((((20.0d * Math.sin((6.0d * d) * Pi)) + (20.0d * Math.sin((2.0d * d) * Pi))) * 2.0d) / 3.0d) + ((((20.0d * Math.sin(Pi * d2)) + (40.0d * Math.sin((d2 / 3.0d) * Pi))) * 2.0d) / 3.0d) + ((((160.0d * Math.sin((d2 / 12.0d) * Pi)) + (320.0d * Math.sin((Pi * d2) / 30.0d))) * 2.0d) / 3.0d);
    }

    private static double transformLon(double d, double d2) {
        return 300.0d + d + (2.0d * d2) + (0.1d * d * d) + (0.1d * d * d2) + (0.1d * Math.sqrt(Math.abs(d))) + ((((20.0d * Math.sin((6.0d * d) * Pi)) + (20.0d * Math.sin((2.0d * d) * Pi))) * 2.0d) / 3.0d) + ((((20.0d * Math.sin(Pi * d)) + (40.0d * Math.sin((d / 3.0d) * Pi))) * 2.0d) / 3.0d) + ((((150.0d * Math.sin((d / 12.0d) * Pi)) + (300.0d * Math.sin((d / 30.0d) * Pi))) * 2.0d) / 3.0d);
    }

    public static double[] wgs2Bd(double d, double d2) {
        double[] Wgs2Gcj = Wgs2Gcj(d, d2);
        return Gcj2Bd(Wgs2Gcj[0], Wgs2Gcj[1]);
    }

}
